package scau.jump.securitytrace.init;

import scau.jump.securitytrace.client.ManagerClient;
import scau.jump.securitytrace.dataModel.AddressDataModel;
import scau.jump.securitytrace.dataModel.ProductDataModel;
import scau.jump.securitytrace.dataModel.RoleDataModel;
import scau.jump.securitytrace.dataModel.UserDataModel;

public class InitContext {
	
	private static final int START = 0;
	private static final int PAGESIZE = 10;
	
	private static UserDataModel userModel = new UserDataModel();
	private static RoleDataModel roleModel = new RoleDataModel();
	private static ProductDataModel productModel = new ProductDataModel();
	private static AddressDataModel addressModel = new AddressDataModel();
	
	private ManagerClient managerClient;
	
	public InitContext(ManagerClient managerClient) {
		Init.initService();
		this.managerClient = managerClient;
	}
	
	public void initDataModel(int pageSize) {
		userModel.setDataVector(Init.getUserService().getListByPage(START, pageSize));
		userModel.setUserTotal((int)this.getTotalPage(Init.getUserService().getTotalCount(),pageSize));
		managerClient.setUserModel(userModel);
		roleModel.setDataVector(Init.getRoleService().getListByPage(START, pageSize));
		roleModel.setRoleTotal((int)this.getTotalPage(Init.getRoleService().getTotalCount(),pageSize));
		managerClient.setRoleModel(roleModel);
		productModel.setDataVector(Init.getProductService().getListByPage(START, pageSize));
		productModel.setProductTotal((int)this.getTotalPage(Init.getProductService().getTotalCount(),pageSize));
		managerClient.setProductModel(productModel);
		addressModel.setDataVector(Init.getAddressService().getListByPage(START, pageSize));
		addressModel.setAddressTotal((int)this.getTotalPage(Init.getAddressService().getTotalCount(),pageSize));
		managerClient.setAddressModel(addressModel);
	}
	
 	private long getTotalPage(long total,int pageSize) {
		if(total%pageSize == 0) {
			return total/pageSize;
		}else {
			return total/pageSize + 1;
		}
	}
	
	public void initDataModel() {
		initDataModel(PAGESIZE);
	}
}
